package com.galen.manual.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.galen.manual.common.Result;
import com.galen.manual.dto.TaskCreateDTO;
import com.galen.manual.dto.TaskQueryDTO;
import com.galen.manual.dto.TaskReviewDTO;
import com.galen.manual.dto.TaskUpdateDTO;
import com.galen.manual.service.TaskService;
import com.galen.manual.vo.TaskListVO;
import com.galen.manual.vo.TaskVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "任务管理", description = "任务相关接口")
@RestController
@RequestMapping("/tasks")
@RequiredArgsConstructor
public class TaskController {

    private final TaskService taskService;

    @Operation(summary = "获取任务列表", description = "分页获取任务列表（管理员用）")
    @GetMapping
    public Result<Page<TaskListVO>> getTaskPage(
            @Parameter(description = "查询条件") TaskQueryDTO queryDTO) {
        Page<TaskListVO> tasks = taskService.getTaskPage(queryDTO);
        return Result.success(tasks);
    }

    @Operation(summary = "获取用户任务列表", description = "分页获取当前用户的任务列表")
    @GetMapping("/my")
    public Result<Page<TaskListVO>> getUserTaskPage(
            @Parameter(description = "查询条件") TaskQueryDTO queryDTO,
            @AuthenticationPrincipal UserDetails userDetails) {
        // 从JWT中获取用户ID，这里暂时写死，实际应该从JWT中解析
        Long userId = 1L; // TODO: 从JWT中获取用户ID
        Page<TaskListVO> tasks = taskService.getUserTaskPage(userId, queryDTO);
        return Result.success(tasks);
    }

    @Operation(summary = "获取任务详情", description = "根据ID获取任务详细信息")
    @GetMapping("/{id}")
    public Result<TaskVO> getTaskDetail(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id) {
        TaskVO task = taskService.getTaskDetail(id);
        return Result.success(task);
    }

    @Operation(summary = "领取任务", description = "创建新任务（领取任务）")
    @PostMapping
    public Result<Long> createTask(
            @Parameter(description = "任务信息", required = true) @Valid @RequestBody TaskCreateDTO createDTO,
            @AuthenticationPrincipal UserDetails userDetails) {
        // 从JWT中获取用户ID，这里暂时写死，实际应该从JWT中解析
        Long userId = 1L; // TODO: 从JWT中获取用户ID
        Long taskId = taskService.createTask(userId, createDTO);
        return Result.success("任务领取成功", taskId);
    }

    @Operation(summary = "更新任务", description = "更新任务信息")
    @PutMapping("/{id}")
    public Result<Void> updateTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "任务信息", required = true) @Valid @RequestBody TaskUpdateDTO updateDTO) {
        taskService.updateTask(id, updateDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "提交任务", description = "提交任务进行审核")
    @PutMapping("/{id}/submit")
    public Result<Void> submitTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id) {
        taskService.submitTask(id);
        return Result.success("任务提交成功");
    }

    @Operation(summary = "审核任务", description = "审核任务（管理员用）")
    @PutMapping("/{id}/review")
    public Result<Void> reviewTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "审核信息", required = true) @Valid @RequestBody TaskReviewDTO reviewDTO,
            @AuthenticationPrincipal UserDetails userDetails) {
        // 从JWT中获取审核人ID，这里暂时写死，实际应该从JWT中解析
        Long reviewerId = 2L; // TODO: 从JWT中获取审核人ID
        taskService.reviewTask(id, reviewerId, reviewDTO);
        return Result.success("任务审核完成");
    }

    @Operation(summary = "回收任务", description = "回收任务（管理员用）")
    @PutMapping("/{id}/reclaim")
    public Result<Void> reclaimTask(
            @Parameter(description = "任务ID", required = true) @PathVariable Long id,
            @Parameter(description = "回收原因", required = true) @RequestParam String reason) {
        taskService.reclaimTask(id, reason);
        return Result.success("任务回收成功");
    }

    @Operation(summary = "批量更新任务状态", description = "批量更新任务状态（管理员用）")
    @PutMapping("/batch-status")
    public Result<Void> updateTaskStatusBatch(
            @Parameter(description = "任务ID列表", required = true) @RequestBody List<Long> taskIds,
            @Parameter(description = "任务状态", required = true) @RequestParam Integer status) {
        taskService.updateTaskStatusBatch(taskIds, status);
        return Result.success("批量更新成功");
    }

    @Operation(summary = "获取用户任务统计", description = "获取用户任务数量统计")
    @GetMapping("/my/count")
    public Result<Integer> getUserTaskCount(
            @Parameter(description = "任务状态", required = true) @RequestParam Integer status,
            @AuthenticationPrincipal UserDetails userDetails) {
        // 从JWT中获取用户ID，这里暂时写死，实际应该从JWT中解析
        Long userId = 1L; // TODO: 从JWT中获取用户ID
        Integer count = taskService.getUserTaskCount(userId, status);
        return Result.success(count);
    }
}